﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

namespace PowerMon
{
    class Sqlhelp
    {
    }
     class SqlHelperParas
    {
        private static string connstr = ConfigurationManager.ConnectionStrings["dbcon"].ConnectionString;
        public static int ExecutenonQuery(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    //foreach (SqlParameter param in parameters)
                    //{
                    //    cmd.Parameters.Add(param);
                    //}
                    cmd.Parameters.AddRange(parameters);


                    return cmd.ExecuteNonQuery();

                }
            }
        }

        public static object ExecuteScalar(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    return cmd.ExecuteScalar();

                }
            }
        }

        public static DataSet ExecuteDateset(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet dataset = new DataSet();

                    adapter.Fill(dataset);



                    return dataset;


                }
            }

        }
        public static DataTable ExecuteDateTable(string sql, params SqlParameter[] parameters)
        {
            using (SqlConnection conn = new SqlConnection(connstr))
            {
                conn.Open();
                using (SqlCommand cmd = conn.CreateCommand())
                {
                    cmd.CommandText = sql;
                    cmd.Parameters.AddRange(parameters);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataSet dataset = new DataSet();
                    adapter.Fill(dataset);
                    //DataTable datatable = new DataTable();
                    //datatable=dataset.Tables[0];




                    return dataset.Tables[0];


                }
            }

        }




    }






     class Customer
     {
         public string Name
         {
             get;
             set;


         }
         public string Address
         {
             get;
             set;
         }
         public string Device
         {
             get;
             set;

         }
         public string Phone
         {
             get;
             set;

         }



     }

     class Devices
     {
         public string DeviceID
         {
             get;
             set;


         }
         public string TempH
         {
             get;
             set;
         }
         public string TempL
         {
             get;
             set;

         }
         public string GPSJ
         {
             get;
             set;

         }

         public string GPSW
         {
             get;
             set;

         }


         public string Time
         {
             get;
             set;

         }



     }






     class SqlHelper
     {
         private static string connstr = ConfigurationManager.ConnectionStrings["dbcon"].ConnectionString;
         public static int ExecutenonQuery(string sql)
         {
             using (SqlConnection conn = new SqlConnection(connstr))
             {
                 conn.Open();
                 using (SqlCommand cmd = conn.CreateCommand())
                 {
                     cmd.CommandText = sql;
                     return cmd.ExecuteNonQuery();

                 }
             }
         }

         public static object ExecuteScalar(string sql)
         {
             using (SqlConnection conn = new SqlConnection(connstr))
             {
                 conn.Open();
                 using (SqlCommand cmd = conn.CreateCommand())
                 {
                     cmd.CommandText = sql;
                     return cmd.ExecuteScalar();

                 }
             }
         }

         public static DataSet ExecuteDateset(string sql)
         {
             using (SqlConnection conn = new SqlConnection(connstr))
             {
                 conn.Open();
                 using (SqlCommand cmd = conn.CreateCommand())
                 {
                     cmd.CommandText = sql;
                     SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                     DataSet dataset = new DataSet();

                     adapter.Fill(dataset);



                     return dataset;


                 }
             }

         }
         public static DataTable ExecuteDateTable(string sql)
         {
             using (SqlConnection conn = new SqlConnection(connstr))
             {
                 conn.Open();
                 using (SqlCommand cmd = conn.CreateCommand())
                 {
                     cmd.CommandText = sql;
                     SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                     DataSet dataset = new DataSet();
                     adapter.Fill(dataset);
                     //DataTable datatable = new DataTable();
                     //datatable=dataset.Tables[0];




                     return dataset.Tables[0];


                 }
             }

         }

     }




}
